package com.woniuxy.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.entity.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json");
        httpServletResponse.setCharacterEncoding("utf-8");

        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.writeValue(httpServletResponse.getWriter(), new ResponseResult(401,null,e.getMessage()));

    }
}
